using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MetroGTD.Model;

namespace MetroGTD.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        readonly IThingsDataContext _dataContext;
        ReadOnlyCollection<CommandViewModel> _commands;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IThingsDataContext dataContext)
        {
            _dataContext = dataContext;
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
        }

        public ObservableCollection<Thing> Things
        {
            get
            {
                return new ObservableCollection<Thing>(_dataContext.GetThings());
            }
        }

        public void UpdateThings()
        {
            RaisePropertyChanged("Things");
        }

        public ReadOnlyCollection<CommandViewModel> Commands
        {
            get
            {
                if (_commands == null)
                {
                    var cmds = CreateCommands();
                    _commands = new ReadOnlyCollection<CommandViewModel>(cmds);
                }

                return _commands;
            }
        }

        List<CommandViewModel> CreateCommands()
        {
            return new List<CommandViewModel>
                       {
                           new CommandViewModel("Today", new RelayCommand(GetThings)),
                           new CommandViewModel("Next", new RelayCommand(GetThings)),
                           new CommandViewModel("Someday", new RelayCommand(GetThings)),
                           new CommandViewModel("Add Thing", new RelayCommand(AddThings))
                       };
        }

        void AddThings()
        {
           Messenger.Default.Send(new DialogMessage(new AddTaskViewModel(new Thing(), _dataContext), null, AddTaskCallback),"AddTask");
          
        }

        void AddTaskCallback(MessageBoxResult result)
        {
            if (result == MessageBoxResult.OK)
            {
                RaisePropertyChanged("Things");
            }
        }

        void GetThings()
        {
            Debug.WriteLine("execute called");
        }
    }
}